package com.senac.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.senac.model.Professor;
import com.senac.model.Titulacao;
import com.senac.service.GerenciamentoEscolaService;
import com.senac.service.ProfessorService;

@WebServlet("/professor")
public class CrudProfessorServlet extends GerenciamentoEscolaAvancado<Professor> {

	private static final long serialVersionUID = -2302968858853427368L;

	@Override
	protected String getPageView() {
		return "pages/professorView.jsp";
	}
	
	@Override
	protected String getPageList() {
		return "pages/professorListar.jsp";
	}
	
	@Override
	protected String getUrlListagem() {
		return "listarProfessores";
	}
	
	@Override
	protected String getUrlCadastro() {
		return "professor";
	}
	
	@Override
	protected String getListAttributeName() {
		return "professores";
	}
	
	@Override
	protected String getViewAttributeName() {
		return "professor";
	}
	
	@Override
	protected GerenciamentoEscolaService<Professor> getService() {
		return new ProfessorService();
	}
	
	@Override
	protected void cadastrar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		addTitulacao(req);
		super.cadastrar(req, resp);
	}
	
	@Override
	protected void editar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		addTitulacao(req);
		super.editar(req, resp);
	}
	
	protected Professor mountByRequest(HttpServletRequest req) {
		Professor professor = new Professor();
		String idString = req.getParameter("id");
		if(idString != null && !idString.equals("")) {
			professor.setId(Integer.parseInt(idString));
		}
		professor.setNome(req.getParameter("nome"));
		String cargaHorariaString = req.getParameter("carga_horaria");
		if(cargaHorariaString != null && !"".equals(cargaHorariaString)) {
			professor.setCargaHoraria(Integer.parseInt(cargaHorariaString));
		} else {
			professor.setCargaHoraria(0);
		}
		String titulacaoString = req.getParameter("titulacao");
		int titulacao = Integer.parseInt(titulacaoString);
		professor.setTitulacao(Titulacao.values()[titulacao]);
		return professor;
	}
	

	private void addTitulacao(HttpServletRequest req) {
		req.setAttribute("titulacoes", Titulacao.values());
	}
	

}
